package br.com.nutriplus.dialog;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JSpinner;
import javax.swing.SwingConstants;

import br.com.nutriplus.dao.ClientJPAController;
import br.com.nutriplus.entity.Client;

import java.awt.Component;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class JDialogClient extends JDialog {

	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private final JPanel contentPanel = new JPanel();
	private JTextField textFieldName;
	private JTextField textFieldPhone;
	private JTextField textFieldMail;

	private Client client = null;
	private JButton okButton;
	private JSpinner spinnerAge;
	private JSpinner spinnerHeight;

	public Client getClient() {
		return client;
	}

	public void setClient(Client client) {
		this.client = client;
	}

	public static void showEditClient(Client client, Component component) {
		JDialogClient dialogClient = new JDialogClient();
		dialogClient.setLocationRelativeTo(component);
		dialogClient.setClient(client);
		dialogClient.setModal(true);
		dialogClient.setVisible(true);
	}

	public static Client showNewClient(Component component) {
		JDialogClient dialogClient = new JDialogClient();
		dialogClient.setLocationRelativeTo(component);

		dialogClient.setModal(true);
		dialogClient.setVisible(true);
		return dialogClient.getClient();
	}

	/**
	 * Create the dialog.
	 */
	private JDialogClient() {
		setTitle("Cliente");
		setBounds(100, 100, 447, 254);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);

		JLabel lblNome = new JLabel("Nome:");
		lblNome.setHorizontalAlignment(SwingConstants.RIGHT);

		textFieldName = new JTextField();
		textFieldName.setColumns(10);

		JLabel lblTelefone = new JLabel("Telefone:");
		lblTelefone.setHorizontalAlignment(SwingConstants.RIGHT);

		textFieldPhone = new JTextField();
		textFieldPhone.setColumns(10);

		JLabel lblEmail = new JLabel("Email:");
		lblEmail.setHorizontalAlignment(SwingConstants.RIGHT);

		textFieldMail = new JTextField();
		textFieldMail.setColumns(10);

		JLabel lblAltura = new JLabel("Altura (cm):");

		spinnerHeight = new JSpinner();

		JLabel lblIdade = new JLabel("Idade:");
		lblIdade.setHorizontalAlignment(SwingConstants.RIGHT);

		spinnerAge = new JSpinner();
		GroupLayout gl_contentPanel = new GroupLayout(contentPanel);
		gl_contentPanel
				.setHorizontalGroup(gl_contentPanel
						.createParallelGroup(Alignment.LEADING)
						.addGroup(
								gl_contentPanel
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												gl_contentPanel
														.createParallelGroup(
																Alignment.LEADING)
														.addGroup(
																Alignment.TRAILING,
																gl_contentPanel
																		.createSequentialGroup()
																		.addGroup(
																				gl_contentPanel
																						.createParallelGroup(
																								Alignment.LEADING)
																						.addComponent(
																								lblNome)
																						.addComponent(
																								lblTelefone)
																						.addComponent(
																								lblEmail))
																		.addPreferredGap(
																				ComponentPlacement.RELATED)
																		.addGroup(
																				gl_contentPanel
																						.createParallelGroup(
																								Alignment.TRAILING)
																						.addComponent(
																								textFieldName,
																								GroupLayout.DEFAULT_SIZE,
																								364,
																								Short.MAX_VALUE)
																						.addComponent(
																								textFieldMail,
																								GroupLayout.DEFAULT_SIZE,
																								364,
																								Short.MAX_VALUE)
																						.addComponent(
																								textFieldPhone,
																								GroupLayout.DEFAULT_SIZE,
																								364,
																								Short.MAX_VALUE)))
														.addGroup(
																gl_contentPanel
																		.createSequentialGroup()
																		.addComponent(
																				lblIdade)
																		.addPreferredGap(
																				ComponentPlacement.RELATED)
																		.addGroup(
																				gl_contentPanel
																						.createSequentialGroup()
																						.addComponent(
																								spinnerAge,
																								GroupLayout.PREFERRED_SIZE,
																								91,
																								GroupLayout.PREFERRED_SIZE)
																						.addPreferredGap(
																								ComponentPlacement.RELATED,
																								93,
																								Short.MAX_VALUE)
																						.addComponent(
																								lblAltura)
																						.addPreferredGap(
																								ComponentPlacement.RELATED)
																						.addComponent(
																								spinnerHeight,
																								GroupLayout.PREFERRED_SIZE,
																								101,
																								GroupLayout.PREFERRED_SIZE))))
										.addContainerGap()));
		gl_contentPanel
				.setVerticalGroup(gl_contentPanel
						.createParallelGroup(Alignment.LEADING)
						.addGroup(
								gl_contentPanel
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												gl_contentPanel
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(lblNome)
														.addComponent(
																textFieldName,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												ComponentPlacement.UNRELATED)
										.addGroup(
												gl_contentPanel
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(
																lblTelefone)
														.addComponent(
																textFieldPhone,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												ComponentPlacement.RELATED)
										.addGroup(
												gl_contentPanel
														.createParallelGroup(
																Alignment.BASELINE)
														.addComponent(lblEmail)
														.addComponent(
																textFieldMail,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												ComponentPlacement.RELATED)
										.addGroup(
												gl_contentPanel
														.createParallelGroup(
																Alignment.LEADING)
														.addGroup(
																gl_contentPanel
																		.createParallelGroup(
																				Alignment.BASELINE)
																		.addComponent(
																				lblAltura)
																		.addComponent(
																				spinnerHeight,
																				GroupLayout.PREFERRED_SIZE,
																				GroupLayout.DEFAULT_SIZE,
																				GroupLayout.PREFERRED_SIZE))
														.addGroup(
																gl_contentPanel
																		.createParallelGroup(
																				Alignment.BASELINE)
																		.addComponent(
																				lblIdade)
																		.addComponent(
																				spinnerAge,
																				GroupLayout.PREFERRED_SIZE,
																				GroupLayout.DEFAULT_SIZE,
																				GroupLayout.PREFERRED_SIZE)))
										.addContainerGap(87, Short.MAX_VALUE)));
		gl_contentPanel.linkSize(SwingConstants.HORIZONTAL, new Component[] {
				lblNome, lblTelefone, lblEmail, lblIdade });
		contentPanel.setLayout(gl_contentPanel);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				okButton = new JButton("OK");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						boolean isUpdate = getClient() != null;
						
						if(!isUpdate){
							setClient(new Client());
						}
						
						ClientJPAController clientJpaController = new ClientJPAController();
						
						if(isUpdate){
							
						}
						else{
							
							updateClientFields();
							ClientJPAController.createClient(getClient());
						
						}
						
						JOptionPane.showMessageDialog(okButton, String.format("Client %s salvo com sucesso", getClient().getName()));
						dispose();
						
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						dispose();
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}
	protected void updateClientFields() {
		
		String name = textFieldName.getText();
		String phone = textFieldPhone.getText();
		String mail = textFieldMail.getText();
		
		int age = (Integer) spinnerAge.getValue();
		int height = (Integer) spinnerHeight.getValue();
		
		getClient().setName(name);
		getClient().setPhone(phone);
		getClient().setEmail(mail);
		getClient().setAge(age);
		getClient().setHeight(height);
	}

	public JButton getOkButton() {
		return okButton;
	}
	public JSpinner getSpinnerAge() {
		return spinnerAge;
	}
	public JSpinner getSpinnerHeight() {
		return spinnerHeight;
	}
}
